package com.drawingtimer.admin.controller;

import com.drawingtimer.admin.model.entity.TMachineInfo;
import com.drawingtimer.admin.model.qo.TMachineInfoQO;
import com.drawingtimer.admin.model.vo.TMachineInfoPageVO;
import com.drawingtimer.admin.service.TMachineInfoService;
import com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 机台信息
 *
 * @author jjh 2025-04-24 16:34:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ProductionProcesses/t-machine-info")
@Tag(name = "机台信息管理")
public class TMachineInfoController {

	private final TMachineInfoService tMachineInfoService;

	/**
	 * 分页查询
	 * @param pageParam 分页参数
	 * @param tMachineInfoQO 机台信息查询对象
	 * @return R 通用返回体
	 */
	@Operation(summary = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-machine-info:read')")
	public R<PageResult<TMachineInfoPageVO>> getTMachineInfoPage(PageParam pageParam, TMachineInfoQO tMachineInfoQO) {
		return R.ok(tMachineInfoService.queryPage(pageParam, tMachineInfoQO));
	}

	/**
	 * 新增机台信息
	 * @param tMachineInfo 机台信息
	 * @return R 通用返回体
	 */
	@Operation(summary = "新增机台信息")
	@CreateOperationLogging(msg = "新增机台信息")
	@PostMapping
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-machine-info:add')")
	public R<Void> save(@RequestBody TMachineInfo tMachineInfo) {
		return tMachineInfoService.save(tMachineInfo) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增机台信息失败");
	}

	/**
	 * 修改机台信息
	 * @param tMachineInfo 机台信息
	 * @return R 通用返回体
	 */
	@Operation(summary = "修改机台信息")
	@UpdateOperationLogging(msg = "修改机台信息")
	@PutMapping
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-machine-info:edit')")
	public R<Void> updateById(@RequestBody TMachineInfo tMachineInfo) {
		return tMachineInfoService.updateById(tMachineInfo) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "修改机台信息失败");
	}

	/**
	 * 通过id删除机台信息
	 * @param machineCode id
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id删除机台信息")
	@DeleteOperationLogging(msg = "通过id删除机台信息")
	@DeleteMapping("/{machineCode}")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-machine-info:del')")
	public R<Void> removeById(@PathVariable("machineCode") String machineCode) {
		return tMachineInfoService.removeById(machineCode) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "通过id删除机台信息失败");
	}

}